package com.duoec.dw.lineage.dto;

import org.springframework.util.CollectionUtils;

import java.util.LinkedHashSet;

/**
 * 血缘关系分析过程状态
 *
 * @author xuwenzhen
 */
public class LineageAnalyseState {
    /**
     * 目标表信息
     */
    private TableInfoDto targetTableDto;

    /**
     * 目标表血缘关系
     */
    private TableLineageDto tableLineage;

    /**
     * 选择器，用于
     */
    private TableFieldLineageDto[] selects;

    public void setTargetTableDto(TableInfoDto targetTableDto) {
        this.targetTableDto = targetTableDto;
        setTargetLineageFields(targetTableDto.getFields());
    }

    public TableInfoDto getTargetTableDto() {
        return targetTableDto;
    }

    public TableLineageDto getTableLineage() {
        return tableLineage;
    }

    public void setTableLineage(TableLineageDto tableLineage) {
        this.tableLineage = tableLineage;
    }

    public TableFieldLineageDto[] getSelects() {
        return selects;
    }

    public void setSelects(TableFieldLineageDto[] selects) {
        this.selects = selects;
    }

    private void setTargetLineageFields(LinkedHashSet<TableFieldDto> fields) {
        if (CollectionUtils.isEmpty(fields)) {
            return;
        }
        LinkedHashSet<TableFieldLineageDto> fieldList = new LinkedHashSet<>();
        fields.forEach(field -> {
            TableFieldLineageDto item = new TableFieldLineageDto();
            item.setIndex(field.getIndex());
            item.setColName(field.getColName());
            item.setComment(field.getComment());
            item.setAlias(field.getAlias());
            fieldList.add(item);
        });
        tableLineage.setFields(fieldList);
    }
}
